package com.group.service.impl;

import com.group.bean.BuildGroupInfoReq;
import com.group.bean.GroupInfo;
import com.group.bean.ResponseMessage;
import com.group.constants.CodeConstants;
import com.group.dao.GroupDao;
import com.group.service.GroupService;
import com.group.utils.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class GroupServiceImpl implements GroupService {

	@Autowired
	private GroupDao groupDao;

	@Autowired
	private JedisUtils jedisUtils  ;

	public static final int CODE_LENGTH = 10;

	public static final String REDIS_PREFIX_GROUP = "GROUP_PHONE";

	/**
	 * 新增团
	 * 
	 * @param buildGroupInfoReq
	 * @return
	 */
	public ResponseMessage buildGroup(BuildGroupInfoReq buildGroupInfoReq) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		String dateStr = sdf.format(new Date());
		String random =  RandomStringUtils.randomNumeric(CODE_LENGTH); // 随机10位数字
		String groupId = buildGroupInfoReq.getGroupName() + dateStr + random;
		String phone = buildGroupInfoReq.getPhone();
		GroupInfo groupInfo = new GroupInfo();
		groupDao.buildGroup(groupInfo);
		long exp = System.currentTimeMillis() + 6 * 3600 * 1000;
		Jedis jedis = jedisUtils.getJedis();
		jedis.set(hashOps.put(REDIS_PREFIX_GROUP + phone, REDIS_PREFIX_GROUP + phone, phone););
		return new ResponseMessage(CodeConstants.CODE_SUCCESS, "新建成功", groupId);
	}

	public ResponseMessage findGroupListByPhone(String phone) {
		List<GroupInfo> groupInfos = groupDao.findGroupListByGroupPhone(phone);
		return new ResponseMessage(CodeConstants.CODE_SUCCESS, "查询成功", groupInfos);
	}
	
	public ResponseMessage findGroupInfoDetailsByGroupId(FindGroupDetailsReq findGroupDetailsReq) {
		return  null;
	}

	
}
